<?php

/**
 * AuthController
 * 
 * @author Maodo DIOP <dmaodo@gmail.com>
 * @version 
 */

require_once 'Zend/Controller/Action.php';
require_once 'Zend/Auth.php';
require_once 'Zend/Auth/Adapter/DbTable.php';

class Admin_ArbreController extends Zend_Controller_Action {
	
	public function indexAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		//$this->view->title = "Supprimer Espece";
		//$this->view->headTitle($this->view->title);
		$arbre = new Admin_Model_DbTable_Matricegmv();
		$res = $arbre->getInfoArbre();
		$resultat = Zend_Json :: encode($res);
		$this->_response->setHeader('Content-Type', 'text/x-json');
		$this->_response->setBody($resultat);
	}
	
	public function addAction() {
		$this->view->title = "Ajouter nouvel espece";
		$this->view->headTitle($this->view->title);
		if ($this->getRequest()->isPost()) {
			$longueur = stripslashes($this->_request->getPost('longueur'));
			$taille = stripslashes($this->_request->getPost('taille'));
			$terrain = stripslashes($this->_request->getPost('terrain'));
			$proprietaire = stripslashes($this->_request->getPost('proprietaire'));
			$origine = stripslashes($this->_request->getPost('origine'));
			$espece = stripslashes($this->_request->getPost('espece'));
			$latitude = stripslashes($this->_request->getPost('latitude'));
			$longitude = stripslashes($this->_request->getPost('longitude'));
			$arbre = new Admin_Model_DbTable_Matricegmv();
			$arbre->addArbre($longueur,$taille,$terrain,$proprietaire,$origine,$espece,$latitude,$longitude);                
			$this->_helper->redirector('index');
		}
	}
	//Action pour ajouter
	public function addIJAction() {
		$this->view->title = "Ajouter nouvel espece";
		$this->view->headTitle($this->view->title);
		
		$i = $this->_getParam('i', 0);
		$j = $this->_getParam('j', 0);
		$arbre = new Admin_Model_DbTable_Matricegmv();
		$arbre->addArbreIJ($i,$j); 
	}
	
	public function editAction() {
		$form = new Admin_Form_Arbre() ;
		$form->submit->setLabel('Enregistrer');
		$this->view->form = $form;
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {            	
				$idarbre = (int)$this->_getParam('idarbre');;             
				$longueur = $form->getValue('longueur');
				$taille = $form->getValue('taille');
				$terrain = $form->getValue('terrain');
				$proprietaire = $form->getValue('proprietaire');
				$origine = $form->getValue('origine');
				$espece = $form->getValue('espece');
				$espece = $form->getValue('latitude');
				$longitude = $form->getValue('longitude');
				
				$arbre = new Admin_Model_DbTable_Matricegmv();
				$arbre->updateArbre($idarbre, $longueur, $taille, $terrain, $proprietaire, $origine, $espece, $latitude, $longitude); 
				$res = array(); 
				$res = $arbre->getArbre($idarbre);        
				$this->_redirect('/admin/arbre/index');
			} else {
				$form->populate($formData);
			}
		} else {
			//Si le formulaire n'est pas encore post�
			//Recuperer la valeur de l'objet � editer
			$idarbre = $this->_request->getParam('idarbre', 0);
			if ($idarbre > 0) {
				$arbre = new Admin_Model_DbTable_Matricegmv();
				$form->populate($arbre->getArbre($idarbre));
			}
		} 
	}
	
	
	public function deleteAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		
		$idarbre = $this->_getParam('idpointgmv', 0);
		$arbre = new Admin_Model_DbTable_Matricegmv();
		$arbre->deleteArbre($idarbre);
		$this->_redirect('/pointgmv/index');
	}
	
	public function allArbreAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		//$this->view->title = "Supprimer Espece";
		//$this->view->headTitle($this->view->title);
		$arbre = new Admin_Model_DbTable_Matricegmv();
		$res = $arbre->getAllArbre();
		$resultat = Zend_Json :: encode($res);
		$this->_response->setBody($resultat);
	}
	
	public function latitudeAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		//$this->view->title = "Supprimer Espece";
		//$this->view->headTitle($this->view->title);
		$arbre = new Admin_Model_DbTable_Matricegmv();
		$res = $arbre->getLatitude($this->_request->getParam('idarbre', 0));
		$resultat = Zend_Json :: encode($res);
		$this->_response->setBody($resultat);
	}
	
	public function longitudeAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		//$this->view->title = "Supprimer Espece";
		//$this->view->headTitle($this->view->title);
		$arbre = new Admin_Model_DbTable_Matricegmv();
		$res = $arbre->getLongitude($this->_request->getParam('idarbre', 0));
		$resultat = Zend_Json :: encode($res);
		$this->_response->setBody($resultat);
	}
	
}

